/*******************************************************************************
 * Copyright (c) 2007 University of Oslo.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     University of Oslo - initial API and implementation
 *******************************************************************************/

package com.cea.papyrus.diagram.sequence.sedi.model.command;

import static com.cea.papyrus.diagram.sequence.sedi.model.util.ModelUtil.createUniqueName;

import java.util.List;

import org.eclipse.uml2.uml.Classifier;
import org.eclipse.uml2.uml.Interaction;
import org.eclipse.uml2.uml.InteractionFragment;
import org.eclipse.uml2.uml.InteractionUse;
import org.eclipse.uml2.uml.Lifeline;
import org.eclipse.uml2.uml.UMLFactory;

import com.cea.papyrus.core.utils.Debug;

/**
 * Command to create instances of InteractionUse.
 * 
 * <p>
 * The following attributes must be set for the command to complete
 * successfully:
 * </p>
 * <ul>
 * <li>Namespace
 * </ul>
 * 
 * <p>
 * The following attributes can be read to access the result of the command:
 * </p>
 * <ul>
 * <li>InteractionUse
 * </ul>
 * 
 * @author Andreas Limyr
 * @author frankd@ifi.uio.no
 */
public class CreateInteractionUseCommand extends AbstractNamespaceCommand {

	private InteractionUse interactionUse;
	private CreateInteractionCommand createInteractionCmd;
	private Interaction owner;
	private List<Lifeline> coveredLifelines;

	/**
	 * 
	 * @return the created {@link InteractionUse}
	 */
	public InteractionUse getInteractionUse() {
		return interactionUse;
	}

	/**
	 * This method should not be used...
	 * use CreateInteractionUseCommand(
	 * 				List<Lifeline> coveredLifelines, 
	 * 				InteractionFragment owner)
	 * 
	 * instead
	 */
	public CreateInteractionUseCommand() {
		super();
		Debug.log(new Exception("This method should not be used."));
	}
	
	/**
	 * Main constructor
	 * @param coveredLifelines the {@link Lifeline}s covered by the new {@link InteractionUse}
	 * @param namespace the {@link Interaction} where the {@link InteractionUse} is created
	 */
	public CreateInteractionUseCommand(List<Lifeline> coveredLifelines, Interaction owner) {
		super();
		this.coveredLifelines = coveredLifelines;
		this.owner = owner;
	}
	
	@Override
	public void execute() {
		createInteractionCmd = new CreateInteractionCommand();
		createInteractionCmd.setNamespace((Classifier) owner.getOwner());
		createInteractionCmd.execute();

		interactionUse = UMLFactory.eINSTANCE.createInteractionUse();
		interactionUse.setName(createUniqueName(owner, "InteractionUse"));
		interactionUse.setRefersTo(createInteractionCmd.getInteraction());
		interactionUse.getCovereds().addAll(coveredLifelines);
		
		owner.getFragments().add(interactionUse);
	}

	@Override
	public void undo() {
		createInteractionCmd.undo();
		getNamespaceFragments().remove(interactionUse);

	}

	@Override
	public void redo() {
		createInteractionCmd.redo();
		List<InteractionFragment> fragments = getNamespaceFragments();
		fragments.add(interactionUse);
	}

	@Override
	public void dispose() {
		if (createInteractionCmd != null)
			createInteractionCmd.dispose();
		createInteractionCmd = null;
		interactionUse = null;
		super.dispose();
	}
}
